<?php

namespace App\Controller\App;

use App\Base\BaseCache;
use App\Base\BaseController4Restful;
use App\Base\Format;
use App\Consts\Date;
use App\Entity\OrderPackage;
use App\Entity\Orders;
use App\Entity\Product;
use App\Entity\Shop;
use App\Entity\ShopStatistic;
use App\Entity\Task;
use App\Entity\User;
use App\Entity\UserAddress;
use App\Entity\UserContact;
use App\Model\LazyCommon\UserModel;
use App\Repository\CategoryRepository;
use App\Repository\OrdersRepository;
use phpDocumentor\Reflection\Type;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @route("/app")
 */
class ShopOrderController extends BaseController4Restful
{
    /**
     * @Route("/shop/orders", name="AppGetShopOrders")
     */
    public function AppGetShopOrders(OrdersRepository $repository)
    {
        $user = $this->getUser();
        //服务订单
        $data["task"] = $repository->findBy(array("seller"=>$user,"type"=>1),array("id"=>"DESC"));
        //任务订单
        $data["serv"] = $repository->findBy(array("seller"=>$user,"type"=>2),array("id"=>"DESC"));
        $this->setGroups("user","task","order");
        return $this->json_out(200,"数据获取成功",$data);
    }
    /**
     * @Route("/user/create/orders/{id}/{UserAddress}/{UserContact}", name="AppUserCreateOrders")
     */
    public function AppUserCreateOrders(Product $product ,UserAddress $address,UserContact $contact)
    {
        $data = $this->getRequestData();
        $user = $this->getUser();
        $this->is->isString($data["tips"]);
        $this->is->isString($data["bookAt"]);
        $this->is->isArray($data["pics"]);

//        dd([$product,$address]);
        $this->myEm()->beginTransaction();
        try {

//        根据商品创建订单
            $price = $product->getPrice();
            $order = new  Orders();
            $order->setBuyer($user)
                ->setSeller($product->getUser())
                ->setCreateAt(new \DateTime())
                ->setSn($this->createOrderSn($user->getId().$product->getUser()->getId()))
                ->setStatus(0)
                ->setType(2)
                ->setPayType(0)
                ->setPayReal($product->getPrice())
                ->setPayTotal($product->getPrice())
                ->setPaCode(md5($this->createOrderSn(rand())))

                ->setContact($contact)
                ->setAddress($address);
            $this->myEm()->persist( $order);

            //根据商品创建一个隐藏任务
            $task = new Task();
            $task->setUser($user)
                ->setCreateAt(new \DateTime())
                ->setOrders($order)
                ->setType(2)
                ->setSn($this->createOrderSn($user->getId().$product->getUser()->getId()))
                ->setContact($contact)
                ->setAddress($address)
                ->setStatus(300)
                ->setBookAt(new \DateTime($data["bookAt"]))
                ->setHireAt(new \DateTime())
                ->setCat1($product->getCat1())
                ->setCat2($product->getCat2())
                ->setTitle($product->getName())
                ->setTips($data["tips"])
                ->setPics($data["pics"])
            ;
            $this->myEm()->persist( $task);

            $OrderPackage = new OrderPackage();
            $OrderPackage->setOrders($order)
                ->setCreateAt(new \DateTime())
                ->setNum(1)
                ->setPrice($product->getPrice())
                ->setEntity($task);
            $this->myEm()->persist( $OrderPackage);
            $this->myEm()->flush();

        } catch (\Exception $exception) {
            $this->myEm()->rollback();
            return  $this->json_out(500,'下单时发生错误',$exception->getLine()."||".$exception->getMessage());
        }
        $this->myEm()->commit();
        return $this->json_out(200,"下单成功,即将发起支付",$data);
    }
}
